import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyFrame extends JFrame {
    public MyFrame() {
        initJFrame();
    }

    private void initJFrame() {
        this.setSize(500, 500);
        this.setLocationRelativeTo(null);
        this.setLayout(null);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);

        JButton btn = new JButton("点我");
        btn.setBounds(0, 0, 100, 100);
//        匿名内部类实现事件监听
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("点我");
            }
        });
        this.getContentPane().add(btn);
    }
}
